%Figure 1
clear variables

load('Fig1_policy.mat')

%% Figure 1 Panel A

figure('units','normalized','outerposition',[0 0 1 1])

[~, tmp_order]=sort(table2array(state_policy(:,2))); %plot states ordered by when stay-in-place was instituted
j=1;

for i=tmp_order'
    %mobility metrics
    tmp_e=plot(us_e_change_min(i), j, 'Marker','o','MarkerFaceColor','red','MarkerEdgeColor','red');
        hold on
    tmp_gr=plot(grocery_max(i), j, 'Marker','o','MarkerFaceColor','yellow',...
        'MarkerEdgeColor','yellow');
    tmp_per=plot(us_e_reopen_percent(i), j, 'Marker','o','MarkerFaceColor','#EDB120',...
            'MarkerEdgeColor','#EDB120'); %reopening 30% encounter rate
    tmp_mo=plot(us_e_reopen_change_min(i), j,'Marker','o','MarkerFaceColor','magenta',...
            'MarkerEdgeColor','magenta');
    
    %policy metrics
    tmp_q=plot(table2array(state_policy(i,2)), j, 'Marker','s','MarkerFaceColor','blue',...
            'MarkerEdgeColor','blue'); %policy - stay in place            
    tmp_qend=plot(table2array(state_policy(i,3)), j, 'Marker','s','MarkerFaceColor','cyan',...
        'MarkerEdgeColor','cyan'); %stay in place lift
    tmp_re=plot(table2array(state_policy(i,4)), j, 'Marker','s','MarkerFaceColor','#77AC30',...
            'MarkerEdgeColor','#77AC30'); %reopening
     
    %adjust marker size for if 2 points overlap
    if us_e_change_min(i)==table2array(state_policy(i,2))
        tmp_q.MarkerSize=5;
        tmp_q.MarkerEdgeColor='red';
    end

    if us_e_reopen_change_min(i)==table2array(state_policy(i,4))
        tmp_re.MarkerSize=5;
        tmp_re.MarkerEdgeColor='magenta';
    end

    if us_e_reopen_percent(i)==table2array(state_policy(i,4))
        tmp_re.MarkerSize=5;
        tmp_re.MarkerEdgeColor='#EDB120'; %reopening 30% encounter rate   
    end
    
    if table2array(state_policy(i,3))==table2array(state_policy(i,4))
        tmp_re.MarkerSize=5;
        tmp_re.MarkerEdgeColor='cyan';
    end
    
    if us_e_reopen_change_min(i)==table2array(state_policy(i,3))
        tmp_qend.MarkerSize=5;
        tmp_qend.MarkerEdgeColor='magenta';
    end
    
    if us_e_reopen_percent(i)==table2array(state_policy(i,3))
        tmp_qend.MarkerSize=5;
        tmp_qend.MarkerEdgeColor='#EDB120';
    end
    
    j=j+1;
end

%national guidelines
tmp_emerg=xline(datetime('3/13/2020','InputFormat','MM/dd/uuuu'),'LineStyle','--','LineWidth',1.5);
tmp_stay1=xline(datetime('3/16/2020','InputFormat','MM/dd/uuuu'),'LineWidth',1.5);
tmp_stay2=xline(datetime('4/30/2020','InputFormat','MM/dd/uuuu'),'LineWidth',1.5);


hold off
grid

set(gca, 'XTick', us_time(12:7:104))
annotation('textbox',[0.1 0 .1 .1],'String','Fri','EdgeColor','none','FontSize',14)

set(gca, 'YTick',1:51, 'YTickLabel',state_abbrev(tmp_order))

legend([tmp_gr tmp_e tmp_mo tmp_per tmp_q tmp_qend tmp_re tmp_emerg tmp_stay1],...
    'Mobility: grocery maximum',...
    'Mobility: quarantine starts changept','Mobility: quarantine stops changept',...
    'Mobility: 30% pre-COVID','Policy: stay-at-home starts',...
    'Policy: stay-at-home stops',...
    'Policy: reopening starts','National state of emergency',...
    'National stay-at-home guidance','NumColumns',1,'FontSize',13)
ax = gca;
yrule = ax.YAxis;
yrule.FontSize = 12;
xrule=ax.XAxis;
xrule.FontSize=14;

ylim([0 52])
xlim([datetime('3/6/2020','InputFormat','MM/dd/uuuu') datetime('6/12/2020','InputFormat','MM/dd/uuuu')])


%% Figure 1 Panel B

figure

bar_graph=zeros(233,4);
[~,tmp1]=ismember(unique(county_e_change_min),us_time);
bar_graph(tmp1,1)=groupcounts(county_e_change_min);

[~,tmp1]=ismember(unique(county_e_reopen_change_min),us_time);
bar_graph(tmp1,2)=groupcounts(county_e_reopen_change_min);

tmp2=unique(county_e_change_fake_min);
[~,tmp1]=ismember(tmp2(1:end-29),us_time); %29 NaTs in fake
tmp2=groupcounts(county_e_change_fake_min);
bar_graph(tmp1,3)=tmp2(1:end-1);

tmp2=unique(county_e_reopen_change_fake_min);
[~,tmp1]=ismember(tmp2(1:end-29),us_time);
tmp2=groupcounts(county_e_reopen_change_fake_min);
bar_graph(tmp1,4)=tmp2(1:end-1);

bar(us_time,bar_graph,1)
ylabel('Number of Counties')
ylim([0 2000])
legend('Lockdown: quarantine starts','Lockdown: quarantine stops','Control: "quarantine starts"',...
    'Control: "quarantine stops"')
